package com.ruoyi.file.component;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.config.RabbitMqConstant;
import com.ruoyi.file.domain.TblFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Component
public class FileConvertorProcessor {
    private final Logger logger = LoggerFactory.getLogger(FileConvertorProcessor.class);
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     *
     * @param tblFile
     * @param type  cad  office
     */
    public void sendConvertFileMsg(TblFile tblFile, String type){
        logger.info("fileConvertorMq send file converter msg type {}   {}...",type,tblFile.getFileName());
        Map<String, Object> map = new HashMap<>();
        map.put("id", tblFile.getId());
        map.put("type", type);//office  cad
        map.put("bucket", tblFile.getBucket());
        map.put("objectName", tblFile.getObjectName());
        map.put("name", tblFile.getFileName());
        map.put("folder", tblFile.getFilePath());
        Message message = MessageBuilder
                .withBody(JSON.toJSONString(map).getBytes(StandardCharsets.UTF_8))
                .setContentEncoding(StandardCharsets.UTF_8.displayName())
                .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .build();
        rabbitTemplate.convertAndSend(RabbitMqConstant.EXCHANGE_FILE_CONVERTOR, "", message);
        logger.info("fileConvertorMq send file converter msg ok...");
    }
}
